package com.trkj.service.hjj;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.trkj.dao.hjj.AttendanceruleDAO_lxlk;
import com.trkj.dao.hjj.CalcluateruleDAO_lxlk;
import com.trkj.dao.hjj.KaoqinruleDAO_lxl;
import com.trkj.mybatisdao.CheckingMapperDao;
import com.trkj.pojo.Attendancerule;
import com.trkj.pojo.Calcluaterule;
import com.trkj.pojo.Kaoqinrule;
import com.trkj.pojo.StaffInformation;
import com.trkj.vo.CheckingVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class CheckingService_lxl {
	@Autowired
	private CheckingMapperDao check;
	@Autowired
	private AttendanceruleDAO_lxlk attendDao;
	@Autowired
	private KaoqinruleDAO_lxl kaoqinDao;
	@Autowired
	private CalcluateruleDAO_lxlk calcDao;
	
	/**
	 * 考勤规则查询
	 * @return
	 */
	public JSONArray findAllChecking(){
		return check.findAllChecking();
	}
	//查询考勤规则结果表
	public JSONArray findAllAttend(){
		return check.findAllAttend();
	}
	//查询计算规则表
	public JSONArray findAllCalcluaterule(){
		return check.findAllCalcluaterule();
	}
	/**
	 * 详情页面规则明细查询
	 * @param kqruleId
	 * @return
	 */
	public JSONArray xqChecking(Long kqruleId){
		return check.xqChecking(kqruleId);
	}
	/**
	 * 编辑传值
	 */
	public Kaoqinrule checkingByValue(Long kqruleId){	
		 return  kaoqinDao.findOne(kqruleId);		
	}
	public JSONArray findKqByName(String kqruleName){
		return check.findKqByName("%"+kqruleName+"%");
	}

	/**
	 * 新增
	 * @param attruId 考勤结果id
	 * @param kqruleId 考勤规则id
	 * @param calcluateruleId 计算规则id
	 * @param kqruleName 考勤规则名称
	 * @param kqruleUp 考勤出勤方式（上班）
	 * @param kqruleDown 考勤出勤方式（下班）
	 * @param kqruleKg 忽略旷工
	 * @param kqruleMarks 备注
	 * @param attruName 考勤结果名称
	 * @param calcluateruleName 计算规则名称
	 * @param calcluateruleCount 计算规则一天工作小时数
	 */
	public void addChecking(String stainName,CheckingVo checkvo){
		Attendancerule attrule = attendDao.findOne(checkvo.getAttruId());
		Calcluaterule calcluater = calcDao.findOne(checkvo.getCalcluateruleId());
		System.out.println("s考勤结果表："+attrule+""+"计算规则表"+calcluater);
		
		Kaoqinrule kaoqing=checkvo.getKaoqinrules();
		
		kaoqing.setKqrulePerson(stainName);
		kaoqing.setKqruleStartdate(new Timestamp(new Date().getTime()));
		kaoqing.setAttendancerule(attrule);
		kaoqing.setCalcluaterule(calcluater);
		
		kaoqinDao.save(kaoqing);
		
	}
	/**
	 * 编辑传值
	 * @param kqruleId
	 * @param checkvo
	 * @return
	 */
	public CheckingVo editByValue(Long kqruleId,CheckingVo checkvo){
		Kaoqinrule kaoqing = kaoqinDao.findOne(kqruleId);
		Attendancerule attrule = attendDao.findOne(checkvo.getAttruId());
		Calcluaterule calcluater = calcDao.findOne(checkvo.getCalcluateruleId());
		checkvo.setKaoqinrules(kaoqing);
		checkvo.setAttendancerule(attrule);
		checkvo.setCalcluaterules(calcluater);
		return checkvo;
	}
	/**
	 * 编辑
	 * @param kqruleId
	 * @param checkvo
	 */
	public void editChecking(Long kqruleId,CheckingVo checkvo){
		Kaoqinrule kaoqing = kaoqinDao.findOne(kqruleId);
		Attendancerule attrule = attendDao.findOne(checkvo.getAttruId());
		Calcluaterule calcluater = calcDao.findOne(checkvo.getCalcluateruleId());
		
		kaoqing.setKqruleName(checkvo.getKaoqinrules().getKqruleName());
		kaoqing.setKqruleUp(checkvo.getKaoqinrules().getKqruleUp());
		kaoqing.setKqruleDown(checkvo.getKaoqinrules().getKqruleDown());
		kaoqing.setKqruleKg(checkvo.getKaoqinrules().getKqruleKg());
		kaoqing.setKqruleMarks(checkvo.getKaoqinrules().getKqruleMarks());
		kaoqing.setKqruleUpdate(new Timestamp(new Date().getTime()));
		kaoqing.setAttendancerule(attrule);
		kaoqing.setCalcluaterule(calcluater);
		
		kaoqinDao.save(kaoqing);
	}
	
	/**
	 * 删除
	 * @param kqruleId
	 */
	public void delete(Long kqruleId){
		try {
			kaoqinDao.delete(kqruleId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
